<?php

namespace app\controllers;

use Yii;
use app\models\HsptLabel;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\filters\VerbFilter;

/**
 * LabelController implements the CRUD actions for HsptLabel model.
 */
class LabelController extends Controller
{
    public $enableCsrfValidation = false;
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all HsptLabel models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => HsptLabel::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single HsptLabel model.
     * @return mixed
     */
    public function actionView()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $result = [];
        $pt_label = explode(" ", Yii::$app->request->post('pt_label'));

        $labels = HsptLabel::find()->where(['in', 'id', $pt_label])->all();
        foreach ($labels as $label) {
            $result[] = ['id' => $label->id, 'name' => $label->name];
        }
        
        return $result;
    }

    /**
     * Creates a new HsptLabel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $name = Yii::$app->request->post('name');

        $model = HsptLabel::find()->where(['name' => $name])->one();
        $model = ($model !== NULL) ? $model : new HsptLabel();

        if ($model->isNewRecord) {
            $model->name = $name;
            if (!$model->save()) {
                return ['code' => '500', 'msg' => '标签保存失败'];
            }
        }
        
        return ['code' => '200', 'msg' => $model->id];
    }

    /**
     * Suggest an existing HsptLabel model.
     * @return mixed
     */
    public function actionGetAll()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $result = [];
        $labels = HsptLabel::find()->all();
        foreach ($labels as $label) {
            $result[] = ['id' => $label->id, 'name' => $label->name];
        }
        
        return $result;
    }

    /**
     * Deletes an existing HsptLabel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the HsptLabel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return HsptLabel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = HsptLabel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
